const {
    pro,
    str_status
} = require('../common/pro_mysql');

const mysql = require('mysql')
var con = mysql.createConnection({
    user:'root',
    // password:'PoralMall123...',
    password:'admin123...',
    database:'poral-mall',
    // host: '47.104.24.155',
    host: 'localhost',
    port: 3306
})
// 获取分类接口
async function GetCategory(req,res){
    let {
        admin_id
    } = req.query;
    if(!admin_id){
        res.status(400).send(str_status(0,'你的admin_id'))
        return
    }
    let sql = `
        SELECT 
        c.ID AS categary_id, 
        c.name AS categary_name, 
        GROUP_CONCAT(JSON_OBJECT('categary_id', cs.ID, 'categary_name', cs.name) SEPARATOR ',') AS categary_list
    FROM 
        categary AS c
        LEFT JOIN categary_small AS cs ON c.ID = cs.categary_id
    WHERE
        c.admin_id = ${admin_id} and cs.admin_id = ${admin_id}
    GROUP BY 
        c.ID
    `;
    let data = await pro(sql);
    res.send(data)
}

// 写入商品和规格
async function SetDuckAndSku(req,res){
    let {
        duck_no_price,
        category_id,
        category_small_id,
        duck_name,
        duck_img,
        duck_img_type,
        duck_img_list,
        duck_min_price,
        duck_max_price,
        sku_color,
        sku_color_title,
        sku_size,
        sku_size_title,
        num,
        desc,
        sku_type,
        admin_id,
    } = req.body;
    duck_img_list = con.escape(JSON.stringify(duck_img_list));
    desc = desc.replace(/>\s+(?=<)/g, '>');
    let sql = 'insert into duck_list (duck_name,duck_createTime,duck_min_price,duck_max_price,duck_no_price,duck_img,duck_img_type,duck_img_list,duck_desc,admin_id,categary_id,categary_small_id) values ("' +duck_name+ '","' +Date.now() +'","' + duck_min_price +'","' + duck_max_price + '","' + duck_no_price+ '","'+ duck_img +'","'+ duck_img_type + '","'+duck_img_list + '",' +  "'" +desc + "'" +',"'+admin_id+'",'+ category_id+','+ category_small_id +')'
    let data = await pro(sql);
    if(data.code == 1){
        sku_color = con.escape(JSON.stringify(sku_color));
        sku_size = con.escape(JSON.stringify(sku_size));
        let skuSql = `insert into duck_sku_list (sku_color_title,sku_color,sku_size_title,sku_size,num,sku_type,duck_id) values ("${sku_color_title}",${sku_color},"${sku_size_title}",${sku_size},${num},${sku_type},${data.result.insertId})`
        let datas = await pro(skuSql);
        res.send(datas);
    }else{
        res.send(data)
    }
}


// 添加分类和子分类
async function SetCategary(req,res){
    let {
        categary_name,
        categary_url,
        admin_id,
        categary_list
    }  = req.body;
    categary_url = con.escape(categary_url);
    let sql = `insert into categary (name,createTime,url,admin_id) values ("${categary_name}","${Date.now()}","${categary_url}",${admin_id})`;
    let data = await pro(sql);
    if(data.code == 1){
        let count = 0,error = [];
        for (const item of categary_list) {
            item.categary_url = con.escape(item.categary_url);
            let sql = `insert into categary_small (name,categary_id,createTime,url,admin_id) values ("${item.categary_name}",${data.result.insertId},"${Date.now()}","${item.categary_url}",${admin_id})`
            let result = await pro(sql);
            if(result.code == 1){
                count ++
            }else{
                error.push(item)
            };
        }
        if(count == categary_list.length){
            res.send(str_status(1,'',[]))
        }else{
            res.send(str_status(0,'',error))
        }
    }else{
        res.send(data)
    }
}

// 写入轮播图
async function SetBanner(req,res){
    let {
        banner_list,
        admin_id
    } = req.body;
    banner_list = con.escape(JSON.stringify(banner_list));
    let sql = `insert into banner (url,createTime,admin_id) values ("${banner_list}",${Date.now()},${admin_id})`
    let data = await pro(sql);
    res.send(data)
}
module.exports = {
    GetCategory,
    SetDuckAndSku,
    SetCategary,
    SetBanner
}